package com.ibm.moa.outmail.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.text.TextUtils;

import com.ibm.moa.outmail.module.MailInfo;

public class ParserRequest {

	/**
	 * 获取收件箱列表
	 * 
	 * @param stream
	 *            JSON输入流
	 * @throws Exception
	 * */
	public static HashMap<String, String> ParsReceiveMailList(String json,
			ArrayList<MailInfo> mails) throws Exception {
		HashMap<String, String> map = new HashMap<String, String>();
		try {
			JSONObject jObject = new JSONObject(json);
			if (jObject != null) {
				if (jObject.has("req_code")) {
					map.put("req_code", jObject.getString("req_code"));
				}
				if (jObject.has("req_mess")) {
					String req_mess = jObject.getString("req_mess");
					if (!TextUtils.isEmpty(req_mess)) {
						if (req_mess.contains("代理")) {
							req_mess = "获取数据失败，请联系手机办公项目组！";
						}
					}
					map.put("req_mess", jObject.getString("req_mess"));
				}
				if (jObject.has("mail_urcnt")) {
					map.put("mail_urcnt", jObject.getString("mail_urcnt"));
				}
				if (jObject.has("mail_cnt")) {
					map.put("mail_cnt", jObject.getString("mail_cnt"));
				}
				if (jObject.has("mail_nmcnt")) {
					map.put("mail_nmcnt", jObject.getString("mail_nmcnt"));
				}
				JSONArray mailArray = jObject.optJSONArray("mail_list");
				if (mailArray != null) {
					MailInfo mailInfo;
					JSONObject mailItem;
					for (int i = 0; i < mailArray.length(); i++) {
						mailInfo = new MailInfo();
						mailItem = mailArray.getJSONObject(i);
						if (mailItem != null) {
							if (mailItem.has("from")) {
								mailInfo.from = mailItem.getString("from");
							}
							if (mailItem.has("haveAttach")) {
								mailInfo.haveAttach = Boolean.parseBoolean(mailItem
										.getString("haveAttach"));
							}
							if (mailItem.has("mailId")) {
								try {
									mailInfo.mailId = mailItem.getString("mailId");
								} catch (Exception e) {
									// TODO: handle exception
								}
								
							}
							if (mailItem.has("mailSize")) {
								try {
									mailInfo.mailSize = Integer.parseInt(mailItem
											.getString("mailSize"));
								} catch (Exception e) {
								}
								
							}
							if (mailItem.has("mailUserId")) {
								mailInfo.mailUserId = mailItem
										.getString("mailUserId");
							}
							if (mailItem.has("readFlag")) {
								mailInfo.readFlag = Utils.parseBoolean(mailItem
										.getString("readFlag"));
							}
							if (mailItem.has("sendDate")) {
								mailInfo.sendDate = mailItem
										.getString("sendDate");
								try {
									SimpleDateFormat day = new SimpleDateFormat(
											"MM月dd日");
									SimpleDateFormat time = new SimpleDateFormat(
											"HH:mm");
									SimpleDateFormat df = new SimpleDateFormat(
											"yyyy-MM-dd HH:mm");
									Date sendDate = df.parse(mailInfo.sendDate);
									Date date = new Date();
									if (date.getMonth() == sendDate.getMonth()
											&& date.getDate() == sendDate
													.getDate()) {
										mailInfo.sendDateFormat = time
												.format(sendDate);
									} else {
										mailInfo.sendDateFormat = day
												.format(sendDate);
									}
								} catch (Exception e) {
									e.printStackTrace();
								}

							}
							if (mailItem.has("subject")) {
								mailInfo.subject = mailItem
										.getString("subject");
							}

							mails.add(mailInfo);
							mailInfo = null;
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(
					"ParserRequest.ParsReceiveMailList is Exception!");
		}
		return map;
	}

	/**
	 * 获取邮件详细信息
	 * 
	 * @param stream
	 *            JSON输入流
	 * @throws Exception
	 * */
	public static HashMap<String, Object> ParsMailInfo(String json)
			throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			JSONObject jObject = new JSONObject(json);
			if (jObject != null) {
				if (jObject.has("req_code")) {
					map.put("req_code", jObject.getString("req_code"));
				}
				if (jObject.has("req_mess")) {
					String req_mess = jObject.getString("req_mess");
					if (!TextUtils.isEmpty(req_mess)) {
						if (req_mess.contains("代理")) {
							req_mess = "获取数据失败，请联系手机办公项目组！";
						}
					}
				}
				JSONObject mailDetail = jObject.optJSONObject("mailDetail");
				if (mailDetail != null) {
					MailInfo mailInfo = new MailInfo();
					if (mailDetail.has("from")) {
						mailInfo.from = mailDetail.getString("from");
					}
					if (mailDetail.has("mailId")) {
						mailInfo.mailId = mailDetail.getString("mailId");
					}
					if (mailDetail.has("mailBcc")) {
						mailInfo.mailBcc = mailDetail.getString("mailBcc");
					}
					if (mailDetail.has("mailCc")) {
						mailInfo.mailCc = mailDetail.getString("mailCc");
					}
					if (mailDetail.has("mailSize")) {
						mailInfo.mailSize = Utils.parseInt(mailDetail.getString("mailSize"));
					}
					if (mailDetail.has("mailUserId")) {
						mailInfo.mailUserId = mailDetail
								.getString("mailUserId");
					}
					if (mailDetail.has("HTMLContent")) {
						mailInfo.plainContent = mailDetail
								.getString("HTMLContent");
						if (mailInfo.plainContent == null
								|| "".equals(mailInfo.plainContent)) {
							if (mailDetail.has("plainContent")) {
								mailInfo.plainContent = mailDetail
										.getString("plainContent");
							}
						}
					} else if (mailDetail.has("plainContent")) {
						mailInfo.plainContent = mailDetail
								.getString("plainContent");
					}
					if (mailDetail.has("readFlag")) {
						mailInfo.readFlag = Utils.parseBoolean(mailDetail.getString("readFlag"));
					}
					if (mailDetail.has("recAddr")) {
						mailInfo.recAddr = mailDetail.getString("recAddr");
					}
					if (mailDetail.has("sendDate")) {
						mailInfo.sendDate = mailDetail.getString("sendDate");
					}
					if (mailDetail.has("subject")) {
						mailInfo.subject = mailDetail.getString("subject");
					}
					JSONArray data = mailDetail.optJSONArray("attachments");
					if (data != null) {
						for (int i = 0; i < data.length(); i++) {
							String item = data.getString(i);
							if (item != null) {
								mailInfo.attachments.add(item);
							}
						}
					}

					map.put("mailInfo", mailInfo);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(
					"ParserRequest.ParsMailInfo is Exception!");
		}
		return map;
	}

	/**
	 * 获取邮件详细信息
	 * 
	 * @param stream
	 *            JSON输入流
	 * @throws Exception
	 * */
	public static HashMap<String, Object> ParsSendMail(String json)
			throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			JSONObject jObject = new JSONObject(json);
			if (jObject != null) {
				if (jObject.has("req_code")) {
					map.put("req_code", jObject.getString("req_code"));
				}
				if (jObject.has("req_mess")) {
					String req_mess = jObject.getString("req_mess");
					if (!TextUtils.isEmpty(req_mess)) {
						if (req_mess.contains("代理")) {
							req_mess = "获取数据失败，请联系手机办公项目组！";
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(
					"ParserRequest.ParsSendMail is Exception!");
		}
		return map;
	}

	/**
	 * 
	 * @param stream
	 *            JSON输入流
	 * @throws Exception
	 * */
	public static HashMap<String, Object> ParsFujianInfo(String json)
			throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			JSONObject jObject = new JSONObject(json);
			if (jObject != null) {
				if (jObject.has("req_code")) {
					map.put("req_code", jObject.getString("req_code"));
				}
				if (jObject.has("req_mess")) {
					String req_mess = jObject.getString("req_mess");
					if (!TextUtils.isEmpty(req_mess)) {
						if (req_mess.contains("代理")) {
							req_mess = "获取数据失败，请联系手机办公项目组！";
						}
					}
				}
				if (jObject.has("file_size")) {
					map.put("file_size", jObject.getString("file_size"));
				}
				if (jObject.has("file_path")) {
					map.put("file_path", jObject.getString("file_path"));
				}
				if (jObject.has("file_name")) {
					map.put("file_name", jObject.getString("file_name"));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(
					"ParserRequest.ParsFujianInfo is Exception!");
		}
		return map;
	}

}
